package main

import (
    "database/sql"
    "fmt"
    "laxkits/lib/laxmysql"
    "laxkits/lib/laxutils"
    "strconv"
    "strings"
)

type XMLNode struct {
    Mysql laxmysql.LaxMySQLConfig `xml:"mysql"`
}

func main() {
    var cfg XMLNode
    if err := laxutils.LoadXmlFile("gate.xml", &cfg); err != nil {
        fmt.Println("读取xml失败")
        return;
    }
    conn, err := laxmysql.NewMySQLConn(&cfg.Mysql)
    if err != nil {
        return
    }
    defer conn.Close()
    type GateNode struct {
        Id     int
        IpAddr string
    }
    var gateList []GateNode
    if err = conn.Query("select server_id, ipaddr from gateinfo where is_open = 1", func(row *sql.Rows) error {
        var gate GateNode
        if err = row.Scan(&gate.Id, &gate.IpAddr); err != nil {
            return err
        }
        gateList = append(gateList, gate)
        return nil
    }); err != nil {
        fmt.Println("读取数据失败,", err)
        return
    }
    for _, gate := range gateList {
        ipaddr := strings.Split(gate.IpAddr, "|")
        if len(ipaddr) > 1 {
            continue
        }
        ipport := strings.Split(gate.IpAddr, ":")
        if len(ipport) < 2 {
            fmt.Println("非法ipaddr", gate.IpAddr)
        }
        port, _ := strconv.Atoi(ipport[1])
        newIpaddr := gate.IpAddr + "|" + ipport[0] + ":" + strconv.Itoa(port+1)
        conn.Execute("update gateinfo set ipaddr=? where server_id = ?", newIpaddr, gate.Id)
    }
}
